home *** CD-ROM | disk | FTP | other *** search
/ Mac Easy 2010 May / Mac Life Ubuntu.iso / casper / filesystem.squashfs / usr / share / perl5 / Debian / DocBase / Programs / Scrollkeeper.pm < prev   
Encoding:
Perl POD Document  |  2008-11-11  |  5.9 KB  |  194 lines

  1. # vim:cindent:ts=2:sw=2:et:fdm=marker:cms=\ #\ %s
  2. #
  3. # $Id: Scrollkeeper.pm 154 2008-11-11 10:14:45Z robert $
  4. #
  5.  
  6. package Debian::DocBase::Programs::Scrollkeeper;
  7.  
  8. use Exporter();
  9. use strict;
  10. use warnings;
  11.  
  12. use vars qw(@ISA @EXPORT);
  13. @ISA = qw(Exporter);
  14. @EXPORT = qw(RegisterScrollkeeper);
  15.  
  16. use Carp;
  17. use Debian::DocBase::Common;
  18. use Debian::DocBase::Utils;
  19. use File::Basename qw(dirname);
  20. use UUID;
  21.  
  22.  
  23.  
  24.  
  25. our $scrollkeeper_update       = "/usr/bin/scrollkeeper-update";
  26. #our $scrollkeeper_gen_seriesid = "/usr/bin/scrollkeeper-gen-seriesid";
  27. our $scrollkeeper_map_file     = "/usr/share/doc-base/data/scrollkeeper.map";
  28.  
  29.  
  30. our %omf_mime_types = (
  31.                             'html'        => 'mime="text/html"',
  32.                             'text'        => 'mime="text/plain"',
  33.                             'pdf'         => 'mime="application/pdf"',
  34.                             'postscript'  => 'mime="application/postscript"',
  35.                             'dvi'         => 'mime="application/x-dvi"',
  36.                             'docbook-xml' => 'mime="text/xml" dtd="-//OASIS//DTD DocBook XML V4.1.2//EN"'
  37.                   );
  38.  
  39. our @omf_formats = (
  40.                         'html',
  41.                         'docbook-xml',
  42.                         'pdf',
  43.                         'postscript',
  44.                         'dvi',
  45.                         'text'
  46.                  );
  47.  
  48. our %mapping = (undef=>undef);
  49.  
  50. sub _GetUUID() { # {{{
  51.   my ($uuid, $retval);
  52.   UUID::generate($uuid);
  53.   UUID::unparse($uuid, $retval);
  54.   return $retval;
  55. } # }}}
  56.  
  57.  
  58. sub RegisterScrollkeeper($@) { # {{{
  59.   my $showinfo  = shift;
  60.   my @documents = @_;
  61.   my $do_update = 0;
  62.  
  63.   Inform("Registering documents with scrollkeeper...")
  64.     if $showinfo and $opt_update_menus and -x $scrollkeeper_update;
  65.  
  66.   Debug("RegisterScrollkeeper started");
  67.  
  68.   # read in doc-base -> scrollkeeper mappings unless already read
  69.   ReadMap($scrollkeeper_map_file, \%mapping);
  70.  
  71.   foreach my $doc (@documents) {
  72.     my $format_data;
  73.  
  74.     my $old_omf_file = $doc->GetStatus('Scrollkeeper-omf-file');
  75.     my $omf_serial_id = undef;
  76.     my $new_omf_file = undef;
  77.     my $omf_category = _MapDocbaseToScrollkeeper($doc->GetSection());
  78.  
  79.     if (defined $omf_category) {
  80.       for my $omf_format (@omf_formats) {
  81.         $format_data = $doc->GetFormat($omf_format);
  82.         next unless defined $format_data;
  83.  
  84.         my $file = defined $$format_data{'index'} ? $$format_data{'index'} : $$format_data{'files'};
  85.         next unless -f $file;
  86.  
  87.         $omf_serial_id = $doc->GetStatus('Scrollkeeper-sid');
  88. #        chomp ($omf_serial_id = `$scrollkeeper_gen_seriesid`) unless defined $omf_serial_id;
  89.         $omf_serial_id =  _GetUUID() unless $omf_serial_id;
  90.         $new_omf_file  = _WriteOmfFile($doc, $file,$omf_format,$omf_category, $omf_serial_id);
  91.         $do_update     = 1;
  92.         last; # register only the first format found
  93.       }
  94.     }
  95.  
  96.     # remove old omf file
  97.     # FIXME: $old_omf_file might be the same file as $new_omf_file even if $old_omf_file ne $new_omf_file
  98.     if (defined $old_omf_file and (not defined $new_omf_file or $old_omf_file ne $new_omf_file)) {
  99.       _RemoveOmfFile($old_omf_file);
  100.       $do_update = 1;
  101.     }
  102.  
  103.     $doc->SetStatus( 'Scrollkeeper-omf-file' => $new_omf_file,
  104.                      'Scrollkeeper-sid'      =>  $omf_serial_id);
  105.   }
  106.  
  107.  
  108.   Execute($scrollkeeper_update, '-q') if ($do_update and $opt_update_menus);
  109.  
  110.  
  111.   Debug("RegisterScrollkeeper finished");
  112. } # }}}
  113.  
  114.  
  115.  
  116.  
  117. # arguments: filename
  118. # reads a file that looks like:
  119. # foo: bar
  120. # returns: hash of lv -> rv
  121.  
  122. # arguments: doc-base section
  123. # returns: scrollkeeper category
  124. sub _MapDocbaseToScrollkeeper($) { # {{{
  125.   return $mapping{lc($_[0])};
  126. } # }}}
  127.  
  128. sub _RemoveOmfFile($) { # {{{
  129.   my $omf_file = shift;
  130.   my $omf_dir = dirname($omf_file);
  131.   Debug("Removing scrollkeeper OMF file `$omf_file'");
  132.   unlink($omf_file) or return Error ("$omf_file: could not delete file: $!");
  133.  
  134.   #check to see if the directory is now empty. if so, kill it.
  135.   if (opendir(DIR, $omf_dir)) {
  136.     if (defined grep { $_ !~ /^\.\.?$/ } readdir DIR) {
  137.       rmdir($omf_dir) or Error ("Could not delete directory `$omf_dir': $!");
  138.     }
  139.     closedir DIR;
  140.   }
  141. } # }}}
  142.  
  143. sub _HTMLEncode($) { # {{{
  144.   my $text = shift;
  145.   $text =~ s/&/(and)/g;     # scrollkeeper doesn't handle & correctly, see Bug#429847
  146.   return HTMLEncode($text);
  147. } # }}}
  148.  
  149. sub _WriteOmfFile($$$$) { # {{{
  150.   my ($doc, $file, $format, $category, $serial_id) = @_;
  151.   my $docid = $doc->GetDocumentID();
  152.   my $omf_file = "$OMF_DIR/$docid/$docid-C.omf";
  153.   my $date;
  154.   my ($sec,$min,$hour,$mday,$mon,$year,$wday,$yday,$isdst) = localtime(time);
  155.   $year += 1900;
  156.   $mon++;
  157.   if ($mday <10) {$mday = "0$mday";}
  158.   if ($mon <10) {$mon = "0$mon";}
  159.   $date = "$year-$mon-$mday";
  160.  
  161.  
  162.   if (! -d "$OMF_DIR/$docid") {
  163.     mkdir("$OMF_DIR/$docid") or croak "Cannot create dir `$OMF_DIR/$docid': $!";
  164.   }
  165.  
  166.   &Debug("Writing scrollkeeper OMF file `$omf_file'");
  167.   open(OMF, ">", $omf_file)
  168.     or croak("Cannot open OMF file `$omf_file' for writing: $!");
  169.  
  170.   #now for the boiler plate XML stuff
  171.   print OMF "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n";
  172.   print OMF "<!DOCTYPE omf PUBLIC \"-//OMF//DTD Scrollkeeper OMF Variant V1.0//EN\" \"http://scrollkeeper.sourceforge.net/dtds/scrollkeeper-omf-1.0/scrollkeeper-omf.dtd\">\n";
  173.   print OMF "<omf>\n\t<resource>\n";
  174.  
  175.   #now for the dynamic stuff
  176.   print OMF "\t\t<creator>".&_HTMLEncode($doc->GetAuthor())."</creator>\n";
  177.   print OMF "\t\t<title>".&_HTMLEncode($doc->GetTitle())."</title>\n";
  178.   print OMF "\t\t<date>$date</date>\n";
  179.   print OMF "\t\t<subject category=\"$category\"/>\n";
  180.   print OMF "\t\t<description>".&_HTMLEncode($doc->GetAbstract())."</description>\n";
  181.   print OMF "\t\t<format $omf_mime_types{$format} />\n";
  182.   print OMF "\t\t<identifier url=\"$file\"/>\n";
  183.   print OMF "\t\t<language code=\"C\"/>\n";
  184.   print OMF "\t\t<relation seriesid=\"$serial_id\"/>\n";
  185.  
  186.   #finish the boiler plate
  187.   print OMF "\t</resource>\n</omf>\n";
  188.   close(OMF) or croak "Cannot close OMF file `$omf_file': $!";
  189.  
  190.   return $omf_file;
  191. } # }}}
  192.  
  193. 1;
  194.